<template>
  <div class="app-nav b-nav">
    <div class="app-nav-title">组织架构</div>
    <div class="app-nav-list">
      <el-tree
        :data="orgtree"
        :props="defaultProps"
        ref="orgElTree"
        @node-click="handleNodeClick"
        :default-expand-all="true"
        :highlight-current="true"
        :expand-on-click-node="false"
      >
        <span class="custom-tree-node" slot-scope="{node,data}">
          <span>
            <font-awesome-icon v-if="data.type=='CORP'" icon="home"/>
            <font-awesome-icon v-else-if="data.type=='DEPT'" icon="users"/>
            {{data.name}}
          </span>
          <span>
            <el-button size="mini" type="text" icon="el-icon-circle-plus-outline" @click.stop="addOrgFrom(data)"></el-button>
            <el-button size="mini" v-if="data.id!=1" type="text" icon="el-icon-delete" @click.stop="delOrg(node,data)"></el-button>
          </span>
        </span>
      </el-tree>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      orgtree: [],
      defaultProps: {
        children: "children",
        label: "name"
      }
    };
  },
  created() {
    this.getAll();
  },
  methods: {
    handleNodeClick(data) {
      this.$emit("getOrg", data);
    },
    getAll() {
      this.$http({
        url: "/core/api/orgs",
        method: "GET"
      }).then(resp => {
        this.orgtree = this.$list2eltree("id", "parentId", resp.data);
        console.debug(this.orgtree);
      });
    },
    addOrgFrom(org) {
      console.debug(org);
    },
    delOrg(node, org) {
      this.$confirm("确认删除组织:" + org.name, "警告", {
        cancelButtonText: "取消",
        confirmButtonText: "确定",
        type: "warning",
        center: true
      }).then(() => {
        // if (org.type == "app") {
          // var appTree = this.$refs.appTree;
          // console.debug(appTree);
          this.$http({
            url: "/core/api/orgs/" + org.id,
            method: "DELETE"
          }).then(() => {
            this.$message({
              type: "success",
              message: "删除成功!"
            });
            this.$refs.orgElTree.remove(node);
          });
        // }
      });
    }
  }
};
</script>
<style scoped>
.custom-tree-node {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-right: 8px;
}
</style>
